/**
 * 
 */
package com.iexbuy.ihg;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;

import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.iexbuy.ihg.common.ExtraKeyConstant;
import com.iexbuy.ihg.wxapi.WeixinPay;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.HttpUtils;

import com.iexbuy.ihg.cache.download.DownFileManager;
import com.iexbuy.ihg.cache.Preferences;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWebPage;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;

import cn.jpush.android.api.JPushInterface;


/**
 * @author chenguiban 全局性的放这里
 *
 */
public class App extends Application {
	private static final String TAG = "Application";
	public static final boolean test = false;
	public static int SCREEN_WIDTH;
	public static int sAPKVersionCode = 0;
	public static String sAPKVersionName = "0.0";
	public static String deviceIdString = "";
	public static List<Activity> sActivityList = null;
	public static Preferences sPreferences;
	public static App sContext;
	public static HttpUtils http = null;
	public static DownFileManager sDownFileManager = null;
	public static TelephonyManager tm;
	public static BitmapUtils bitmapUtils = null;
	public static String EXTRA_ACCESS_TOKEN = "";
	public static DbUtils db;
	public static UMSocialService umSocialService;
	public static LocationClient mLocationClient;
	public static MyLocationListener mMyLocationListener;

	public void onCreate() {
		Log.v(getClass().getSimpleName(), "onTerminate()");
		super.onCreate();
		sContext = this;
		init();
	}
	
	@Override
    public void onTerminate() {
        Log.v(getClass().getSimpleName(), "onTerminate()");
        super.onTerminate();
        App.sPreferences.setLoginIn(false);
        App.sPreferences.sethasUpdate(false);
    }
	
	public void init() {
		SDKInitializer.initialize(getApplicationContext());
		initProperties();
		getScreenSize();
		initApkVersionInfo();
		
		db = DbUtils.create(getApplicationContext());
	}
	
	private void getScreenSize() {
		DisplayMetrics displaymetrics = getResources().getDisplayMetrics();
		SCREEN_WIDTH = displaymetrics.widthPixels;
	}
	
	private void initProperties() {
		sActivityList = new ArrayList<Activity>();
		sPreferences = Preferences.getInstance(getApplicationContext());
		sDownFileManager = DownFileManager.getInstance();
		http = new HttpUtils();
		tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
		deviceIdString = tm.getDeviceId();

		bitmapUtils = new BitmapUtils(this);
		bitmapUtils.configDiskCacheEnabled(true);
		bitmapUtils.configMemoryCacheEnabled(false);

		setupShareConfig();

		mLocationClient = new LocationClient(this.getApplicationContext());
		initLocationClient();
		mMyLocationListener = new MyLocationListener();
		mLocationClient.registerLocationListener(mMyLocationListener);
		mLocationClient.requestLocation();

		JPushInterface.setDebugMode(true);
		JPushInterface.init(this);
	}

	private void initLocationClient() {
		LocationClientOption option  = new LocationClientOption();
		option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
		option.setCoorType("bd09ll");
		option.setOpenGps(true);
		mLocationClient.setLocOption(option);
	}

	private void setupShareConfig() {
		String shareTtile = "爱换购分享";
		UMWXHandler wxHandler = new UMWXHandler(getApplicationContext(), WeixinPay.APP_ID, WeixinPay.APP_SECRET);
		wxHandler.setTitle(shareTtile);
		wxHandler.addToSocialSDK();
		UMWXHandler wxCircleHandler = new UMWXHandler(getApplicationContext(), WeixinPay.APP_ID, WeixinPay.APP_SECRET);
		wxCircleHandler.setTitle(shareTtile);
		wxCircleHandler.setToCircle(true);
		wxCircleHandler.addToSocialSDK();
		umSocialService = UMServiceFactory.getUMSocialService("com.umeng.share");

		//分享给朋友
		WeiXinShareContent weixinContent = new WeiXinShareContent();
		weixinContent.setTitle(shareTtile);
		weixinContent.setShareContent("你的朋友向你推荐爱换购,可消费免单喔，赶快下载享受优惠吧。");
		weixinContent.setTargetUrl("http://www.iexbuy.com/download/app.html");
		weixinContent.setShareImage(new UMImage(getApplicationContext(), "http://www.iexbuy.com/ic_launcher.png"));
		umSocialService.setShareMedia(weixinContent);

		//设置微信朋友圈分享内容
		CircleShareContent circleMedia = new CircleShareContent();
		circleMedia.setShareContent("你的朋友向你推荐爱换购,可消费免单喔，赶快下载享受优惠吧。");
		circleMedia.setTitle(shareTtile);
		circleMedia.setTargetUrl("http://www.iexbuy.com/download/app.html");
		circleMedia.setShareImage(new UMImage(getApplicationContext(), "http://www.iexbuy.com/ic_launcher.png"));
		umSocialService.setShareMedia(circleMedia);
	}

	private void initApkVersionInfo() {
		try {
			String packageName = sContext.getPackageName();
			sAPKVersionCode = sContext.getPackageManager().getPackageInfo(packageName, 0).versionCode;
			sAPKVersionName = sContext.getPackageManager().getPackageInfo(packageName, 0).versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 实现实时位置回调监听
	 */
	public class MyLocationListener implements BDLocationListener {

		@Override
		public void onReceiveLocation(BDLocation location) {
			//Receive Location
			App.sPreferences.setLatitude(String.valueOf(location.getLatitude()));
			App.sPreferences.setLongitude(String.valueOf(location.getLongitude()));

			App.sPreferences.setCityName(location.getCity());
			App.sPreferences.setCityCode(location.getCityCode());
		}
	}


}
